package com.hit.aircraftwar.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.hit.aircraftwar.R;
import com.hit.aircraftwar.httputil.MyWsManager;
import com.hit.aircraftwar.setter.ParameterSet;

import java.util.Objects;

public class RoomActivity extends AppCompatActivity {
    private static final String TAG = "RoomActivity";
    private static RoomActivity roomActivity;
    private final String url = "http://426k04070x.qicp.vip/user";

    @SuppressLint("HandlerLeak")
    private final Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            int arg1 = msg.arg1;
            String info = (String) msg.obj;
            if (msg.what == 1){
                Toast toast = Toast.makeText(RoomActivity.roomActivity,"匹配成功",Toast.LENGTH_SHORT);
                toast.show();

                //进行游戏的跳转
                ParameterSet.INSTANCE.isOnline = true;
                ParameterSet.INSTANCE.setDifficulty(2);
                Intent intent = new Intent(RoomActivity.this, GameActivity.class);
                startActivity(intent);

            }else if (msg.what == 2){
                //进行别的处理
                Toast toast = Toast.makeText(RoomActivity.roomActivity,"请稍后，正在匹配中",Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        roomActivity = this;
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Objects.requireNonNull(getSupportActionBar()).hide();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room);

        //创建连接
        MyWsManager.getInstance().init(this, handler);


        Button startMatchingButton;
        Button exitMatchingButton;

        //Button定义
        startMatchingButton = findViewById(R.id.matchingButton);
        exitMatchingButton = findViewById(R.id.exitMatchingButton);

        exitMatchingButton.setOnClickListener(view -> {
            //关闭连接
            MyWsManager.getInstance().disconnect();
            Intent intent = new Intent(RoomActivity.this, MainActivity.class);
            startActivity(intent);
        });

        startMatchingButton.setOnClickListener(view -> {
            //发送匹配请求
             MyWsManager.getInstance().sendData("匹配");
        });
    }

//    public static  Handler getHandler() {
//        return handler;
//    }
}
